#pragma once

#include <iostream>

using namespace std;

namespace bit
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _prev;
		ListNode<T>* _next;

		T _data;

		ListNode(const T& data = T())
			:_data(data),
			_prev(nullptr),
			_next(nullptr)
		{}
	};

	template<class T,class Ref,class Ptr>
	class ListIterator
	{
		typedef ListNode<T> Node;
		typedef ListIterator<T> self;

	public:
		Node* _node;

		ListIterator(Node* node)
			:_node(node)
		{}

		self& operator++()
		{
			_node = _node->_next;

			return *this;
		}

		self& operator--()
		{
			_node = _node->_prev;

			return *this;
		}

		self operator--(int)
		{
			self tmp = *this;

			_node = _node->_prev;

			return tmp;
		}

		self operator++(int)
		{
			self tmp = *this;

			_node = _node->_next;

			return tmp;
		}

		bool operator!=(const self& node)
		{
			return _node != node._node;
		}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data
		}
	};

	/*template<class T>
	class ConstListIterator
	{
		typedef ListNode<T> Node;
		typedef ConstListIterator<T> self;

	public:
		Node* _node;

		ConstListIterator(Node* node)
			:_node(node)
		{}

		self& operator++()
		{
			_node = _node->_next;

			return *this;
		}

		self& operator--()
		{
			_node = _node->_prev;

			return *this;
		}

		self operator--(int)
		{
			self tmp = *this;

			_node = _node->_prev;

			return tmp;
		}

		self operator++(int)
		{
			self tmp = *this;

			_node = _node->_next;

			return tmp;
		}

		bool operator!=(const self& node)
		{
			return _node != node._node;
		}

		const T& operator*()
		{
			return _node->_data;
		}

		const T* operator->()
		{
			return &_node->_data
		}
	};*/

	template<class T>
	class list
	{
		typedef ListNode<T> Node;

	public:

		typedef ListIterator<T,T&,T*> iterator;
		//typedef ConstListIterator<T> const_iterator;
		typedef ListIterator<T, const T&,const T*> const_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void push_back(const T& x)
		{
			Node* newnode = new Node(x);
			Node* tail = _head->_prev;

			newnode->_prev = tail;
			newnode->_next = _head;

			tail->_next = newnode;
			_head->_prev = newnode;
		}

		void insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;

			Node* prev = cur->_prev;
			Node* newnode = new Node(x);

			newnode->_prev = prev;
			newnode->_next = cur;

			prev->_next = newnode;
			cur->_prev = newnode;
		}

		void erase(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur;
		}
	private:
		Node* _head;
	};
}


